package com.qian.service.ddj.serviceImpl;

import com.qian.api.ddj.model.BusinessCard;
import com.qian.api.ddj.service.IBusinessCardService;
import com.qian.service.ddj.dao.BusinessCardRepository;
import com.qian.service.ddj.dao.UserRepository;
import com.qian.service.ddj.entity.DdjBusiness;
import com.qian.service.ddj.entity.DdjBusinessCard;
import com.qian.service.ddj.entity.DdjBusinessGroup;
import com.qian.service.ddj.entity.DdjUser;
import com.yaowk.service.common.BaseRepository;
import com.yaowk.service.common.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class BusinessCardServiceImpl extends BaseServiceImpl<BusinessCard, DdjBusinessCard, Integer> implements IBusinessCardService {

    @Autowired
    private BusinessCardRepository dao;
    @Autowired
    private UserRepository userDao;


    @Override
    protected BaseRepository getDao() {
        return dao;
    }

    @Override
    protected DdjBusinessCard model2Entity(BusinessCard model) {
        DdjBusinessCard businessCard = null;
        if (isNotEmpty(model)) {
            businessCard = new DdjBusinessCard();
            businessCard.setId(model.getId());
            businessCard.setBalance(model.getBalance());
            if (isNotEmpty(model.getBusinessGroupId())) {
                DdjBusinessGroup businessGroup = new DdjBusinessGroup();
                businessGroup.setId(model.getBusinessGroupId());
                businessCard.setBusinessGroup(businessGroup);
            }
            businessCard.setCardBank(model.getCardBank());
            businessCard.setCardNo(model.getCardNo());
            businessCard.setCardPerson(model.getCardPerson());
            businessCard.setCardPhone(model.getCardPhone());
            businessCard.setRemark(model.getRemark());
        }
        return businessCard;
    }

    @Override
    protected BusinessCard entity2model(DdjBusinessCard entity) {
        BusinessCard businessCard = null;
        if (isNotEmpty(entity)) {
            businessCard = new BusinessCard();
            businessCard.setId(entity.getId());
            businessCard.setBalance(entity.getBalance());
            businessCard.setCardBank(entity.getCardBank());
            businessCard.setCardNo(entity.getCardNo());
            businessCard.setCardPerson(entity.getCardPerson());
            businessCard.setCardPhone(entity.getCardPhone());
            businessCard.setRemark(entity.getRemark());
        }
        return businessCard;
    }

    @Override
    public List<BusinessCard> findByUserId(Integer userId) {
        Set<DdjBusinessCard> businessCards = new HashSet<>();
        DdjUser ddjUser = userDao.findOne(userId);
        Set<DdjBusiness> businessSet = ddjUser.getBusinesses();
        for (DdjBusiness ddjBusiness : businessSet) {
            DdjBusinessCard ddjBusinessCard = ddjBusiness.getBusinessCard();
            if (isNotEmpty(ddjBusinessCard))
                businessCards.add(ddjBusinessCard);
        }
        return entity2Model(businessCards);
    }

    @Override
    public BusinessCard save(BusinessCard model) {
        if (model.isNew()) {
            model = simpleSave(model);
        } else {
            DdjBusinessCard ddjBusinessCard = dao.findOne(model.getId());
            if (isNotEmpty(model.getBalance()))
                ddjBusinessCard.setBalance(model.getBalance());
            if (isNotEmpty(model.getCardBank()))
                ddjBusinessCard.setCardBank(model.getCardBank());
            if (isNotEmpty(model.getCardNo()))
                ddjBusinessCard.setCardNo(model.getCardNo());
            if (isNotEmpty(model.getCardPerson()))
                ddjBusinessCard.setCardPhone(model.getCardPhone());
            if (isNotEmpty(model.getCardPhone()))
                ddjBusinessCard.setCardPhone(model.getCardPhone());
            if (isNotEmpty(model.getRemark()))
                ddjBusinessCard.setRemark(model.getRemark());
            model = entity2model(dao.save(ddjBusinessCard));
        }
        return model;
    }
}
